package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class VUserInfoPeer extends BasePeer{
	public static final String tableName = "V_User_Info";
	public static final String USERID = "userId";
	public static final String USERNAME = "username";
	public static final String PRIMARYEMAIL = "primaryEmail";
	public static final String PASSWORD = "password";
	public static final String STATUS = "status";
	public static final String ACCOUNTSTATE = "accountState";
	public static final String ONLINESTATE = "onlineState";
	public static final String LOGINTOKEN = "loginToken";
	public static final String TOKENEXPIRED = "tokenExpired";
	public static final String REGISTERTIME = "registerTime";
	public static final String LASTACTIVETIME = "lastActiveTime";
	public static final String LASTLOGINTIME = "lastLoginTime";
	public static final String LASTLOGOUTTIME = "lastLogoutTime";
	public static final String TOTALONLINEMINUTES = "totalOnlineMinutes";
	public static final String REALNAME = "realName";
	public static final String NICENAME = "nicename";
	public static final String ICON = "icon";
	public static final String EMAIL = "email";
	public static final String DESCRIPTION = "description";
	public static final String IM = "im";
	public static final String PHONE = "phone";
	public static final String FAX = "fax";
	public static final String MOBILE = "mobile";
	public static final String WEBSITE = "website";
	public static final String OTHER = "other";
	public static final String PRIVACYPOLICY = "privacyPolicy";
	public static final String[] columnNames=new String[]{"userId","username","primaryEmail","password","status","accountState","onlineState","loginToken","tokenExpired","registerTime","lastActiveTime","lastLoginTime","lastLogoutTime","totalOnlineMinutes","realName","nicename","icon","email","description","im","phone","fax","mobile","website","other","privacyPolicy"};
	public static VUserInfo getVUserInfoByPKs( Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(VUserInfoPeer.tableName,VUserInfoPeer.columnNames," TRUE ",new Object[]{},0, 1,conn);
			VUserInfo vUserInfo = new VUserInfo();
		fill(rs,vUserInfo);
		return vUserInfo;
	}
	public static VUserInfo getVUserInfoReferenceColumnsOnlyByPKs( Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(VUserInfoPeer.tableName,new String[]{}," TRUE ",new Object[]{}, 0,  1,conn);
			VUserInfo vUserInfo = new VUserInfo();
		fill(rs,vUserInfo);
		return vUserInfo;
	}
	public static void fill(ResultSet rs, VUserInfo vUserInfo) throws SQLException {
		if(rs.next()){
			try{vUserInfo.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{vUserInfo.setUsername(rs.getString("username"));}catch(Exception ex){}
			try{vUserInfo.setPrimaryEmail(rs.getString("primaryEmail"));}catch(Exception ex){}
			try{vUserInfo.setPassword(rs.getString("password"));}catch(Exception ex){}
			try{vUserInfo.setStatus(rs.getString("status"));}catch(Exception ex){}
			try{vUserInfo.setAccountState(rs.getString("accountState"));}catch(Exception ex){}
			try{vUserInfo.setOnlineState(rs.getString("onlineState"));}catch(Exception ex){}
			try{vUserInfo.setLoginToken(rs.getString("loginToken"));}catch(Exception ex){}
			try{vUserInfo.setTokenExpired(rs.getBoolean("tokenExpired"));}catch(Exception ex){}
			try{vUserInfo.setRegisterTime(rs.getTimestamp("registerTime"));}catch(Exception ex){}
			try{vUserInfo.setLastActiveTime(rs.getTimestamp("lastActiveTime"));}catch(Exception ex){}
			try{vUserInfo.setLastLoginTime(rs.getTimestamp("lastLoginTime"));}catch(Exception ex){}
			try{vUserInfo.setLastLogoutTime(rs.getTimestamp("lastLogoutTime"));}catch(Exception ex){}
			try{vUserInfo.setTotalOnlineMinutes(rs.getInt("totalOnlineMinutes"));}catch(Exception ex){}
			try{vUserInfo.setRealName(rs.getString("realName"));}catch(Exception ex){}
			try{vUserInfo.setNicename(rs.getString("nicename"));}catch(Exception ex){}
			try{vUserInfo.setIcon(rs.getLong("icon"));}catch(Exception ex){}
			try{vUserInfo.setEmail(rs.getString("email"));}catch(Exception ex){}
			try{vUserInfo.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{vUserInfo.setIm(rs.getString("im"));}catch(Exception ex){}
			try{vUserInfo.setPhone(rs.getString("phone"));}catch(Exception ex){}
			try{vUserInfo.setFax(rs.getString("fax"));}catch(Exception ex){}
			try{vUserInfo.setMobile(rs.getString("mobile"));}catch(Exception ex){}
			try{vUserInfo.setWebsite(rs.getString("website"));}catch(Exception ex){}
			try{vUserInfo.setOther(rs.getString("other"));}catch(Exception ex){}
			try{vUserInfo.setPrivacyPolicy(rs.getString("privacyPolicy"));}catch(Exception ex){}
			vUserInfo.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<VUserInfo> vUserInfoCollection) throws SQLException {
		while(rs.next()){
			VUserInfo vUserInfo=new VUserInfo();
			try{vUserInfo.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{vUserInfo.setUsername(rs.getString("username"));}catch(Exception ex){}
			try{vUserInfo.setPrimaryEmail(rs.getString("primaryEmail"));}catch(Exception ex){}
			try{vUserInfo.setPassword(rs.getString("password"));}catch(Exception ex){}
			try{vUserInfo.setStatus(rs.getString("status"));}catch(Exception ex){}
			try{vUserInfo.setAccountState(rs.getString("accountState"));}catch(Exception ex){}
			try{vUserInfo.setOnlineState(rs.getString("onlineState"));}catch(Exception ex){}
			try{vUserInfo.setLoginToken(rs.getString("loginToken"));}catch(Exception ex){}
			try{vUserInfo.setTokenExpired(rs.getBoolean("tokenExpired"));}catch(Exception ex){}
			try{vUserInfo.setRegisterTime(rs.getTimestamp("registerTime"));}catch(Exception ex){}
			try{vUserInfo.setLastActiveTime(rs.getTimestamp("lastActiveTime"));}catch(Exception ex){}
			try{vUserInfo.setLastLoginTime(rs.getTimestamp("lastLoginTime"));}catch(Exception ex){}
			try{vUserInfo.setLastLogoutTime(rs.getTimestamp("lastLogoutTime"));}catch(Exception ex){}
			try{vUserInfo.setTotalOnlineMinutes(rs.getInt("totalOnlineMinutes"));}catch(Exception ex){}
			try{vUserInfo.setRealName(rs.getString("realName"));}catch(Exception ex){}
			try{vUserInfo.setNicename(rs.getString("nicename"));}catch(Exception ex){}
			try{vUserInfo.setIcon(rs.getLong("icon"));}catch(Exception ex){}
			try{vUserInfo.setEmail(rs.getString("email"));}catch(Exception ex){}
			try{vUserInfo.setDescription(rs.getString("description"));}catch(Exception ex){}
			try{vUserInfo.setIm(rs.getString("im"));}catch(Exception ex){}
			try{vUserInfo.setPhone(rs.getString("phone"));}catch(Exception ex){}
			try{vUserInfo.setFax(rs.getString("fax"));}catch(Exception ex){}
			try{vUserInfo.setMobile(rs.getString("mobile"));}catch(Exception ex){}
			try{vUserInfo.setWebsite(rs.getString("website"));}catch(Exception ex){}
			try{vUserInfo.setOther(rs.getString("other"));}catch(Exception ex){}
			try{vUserInfo.setPrivacyPolicy(rs.getString("privacyPolicy"));}catch(Exception ex){}
			vUserInfo.resetModified();
			vUserInfoCollection.add(vUserInfo);
		}
	}
	public static boolean update(VUserInfo vUserInfo, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < VUserInfoPeer.columnNames.length; i++) {
			if (((VUserInfoBase)vUserInfo).modified[i]) {
				columns.add(VUserInfoPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `V_User_Info` SET ");
		sql.append("WHERE TRUE ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,vUserInfo.get(column));
		}
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(VUserInfo vUserInfo, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (vUserInfo.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `V_User_Info` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (vUserInfo.get(column) != null) {
				pstmt.setObject(i + 1 + j, vUserInfo.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<VUserInfo> getVUserInfoCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<VUserInfo> collection=new Vector<VUserInfo>();
		fill(rs,collection);
		return collection;
	}
}
